package com.itheima.controller.backend;

import com.itheima.common.constant.MessageConstant;
import com.itheima.common.entity.PageResult;
import com.itheima.common.entity.QueryPageBean;
import com.itheima.common.entity.Result;
import com.itheima.common.utils.AliOssUtil;
import com.itheima.pojo.Setmeal;
import com.itheima.service.backend.SetmealService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.UUID;

/**
 * 套餐管理
 */
@RestController
@RequestMapping( "/setmeal")
@Slf4j
public class SetmealController {

    @Autowired
    private SetmealService setmealService;
    /**
     * 阿里云对象存储工具类
     */
    @Autowired
    private AliOssUtil aliOssUtil;
    /**
     * RedisTemplate
     */
    @Autowired
    private RedisTemplate redisTemplate;

    /**
     * 新增套餐
     * @param setmeal
     * @param checkgroupIds
     * @return
     */
    @PostMapping("/add")
    public Result add(@RequestBody Setmeal setmeal, Integer[] checkgroupIds){
        log.info("新增套餐：{}",setmeal);
        try {
            //新增套餐
            setmealService.add(setmeal, checkgroupIds);
            //套餐数据插入到数据库后，将图片名称保存到另一个redis集合中
            redisTemplate.opsForSet().add("setmealPicSetDb", setmeal.getImg());
            //生成套餐列表静态页面
            setmealService.createSetmealPage();
            //生成套餐详情静态页面
            setmealService.createSetmealDetailPage();
            return new Result(true, "新增套餐成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, "新增套餐失败");
        }
    }

    /**
     * 编辑套餐
     * @param setmeal
     * @param checkGroupIds
     * @return
     */
    @PostMapping("/edit")
    public Result edit(@RequestBody Setmeal setmeal, Integer[] checkGroupIds){
        log.info("编辑套餐：{}", setmeal);
        try {
            setmealService.edit(setmeal, checkGroupIds);
            return new Result(true, "编辑套餐成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, "编辑套餐失败");
        }
    }

    /**
     * 删除套餐
     * @param id
     * @return
     */
    @GetMapping("/deleteById")
    public Result delete(Integer id){
        log.info("删除套餐：{}", id);
        try {
            setmealService.delete(id);
            return new Result(true, "删除套餐成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, "删除套餐失败");
        }
    }

    /**
     * 分页查询
     * @param queryPageBean
     * @return
     */
    @PostMapping("/findPage")
    public PageResult findPage(@RequestBody QueryPageBean queryPageBean) {
        log.info("分页查询套餐：{}", queryPageBean);
        return setmealService.findPage(queryPageBean);
    }

    /**
     * 根据id查询套餐
     * @param id
     * @return
     */
    @GetMapping("/findById")
    public Result findById(Integer id){
        log.info("根据id查询套餐：{}", id);
        try {
            return new Result(true, "查询成功", setmealService.findById(id));
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, "查询失败");
        }
    }

    /**
     * 上传图片
     * @param imgFile
     * @return
     */
    @PostMapping("upload")
    public Result upload(@RequestParam(value = "imgFile") MultipartFile imgFile){
        log.info("文件上传：{}",imgFile);
        try {
            //原始文件名
            String originalFilename = imgFile.getOriginalFilename();
            //截取原始文件名的后缀
            String extension = originalFilename.substring(originalFilename.lastIndexOf("."));
            //构造新文件名称
            String objectName = UUID.randomUUID().toString() + extension;

            //文件的请求路径
            String filePath = aliOssUtil.upload(imgFile.getBytes(), objectName);

            //文件上传成功后，将图片名称保存到一个redis集合中
            redisTemplate.opsForSet().add("setmealPicSetAli", filePath);

            return new Result(true, MessageConstant.PIC_UPLOAD_SUCCESS, filePath);
        } catch (IOException e) {
            log.error("文件上传失败：{}", e);
        }
        return new Result(false, MessageConstant.PIC_UPLOAD_FAIL);
    }
}
